<?php
/**
 * alltosun.com admin_widget
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2016-3-9 下午3:21:53 $
 * $Id: admin_data.php 191750 2016-05-30 05:12:34Z gaoa $
 */

class admin_data_widget
{
    /**
     *
     * 合伙人相关统计信息
        1.合伙人数量：（要求排除不统计测试账号实际目前就6个）
        2.今日新增：多少人增长率多少
        3.成交订单数/已收到款项：:订单数量/（成交金额）多少元 (确认收款)
        4.已成功出货：多少箱 (发货)
        5.待收款订单/等待收款项：订单数量/（未成交金额）多少元
        6.等待发货：多少箱 (只要提交订单就算是)
        7.合伙人总库存：多少箱
        8.合伙人总员工：多少人 (市场人员的总数量)


        终端门店相关统计信息
        1.终端门店总数量
        2.今日新增：多少门店增长率多少
        3.已进货门店数量：(必须是确认收货的门店)
        4.未进货门店数量：(其他)
        5.门店订单总量：(所有订单)
        6.今日新增订单量：(统计处在配送中的订单)
     */
    public function get_admin_data_list()
    {
        $admin_data_list = array();

        // 总的用户数
        // $admin_data_list['all_user_num'] = user_helper::get_user_num_by_role_id();

        // 经销商相关数据
        // 经销商数量
        $admin_data_list['dealer_data']['dealer_num'] = user_helper::get_user_num_by_role_id(user_config::AGENCY);

        // 今日新增：多少人增长率多少
        $today_add_dealer_num = user_helper::get_user_num_by_role_id(user_config::AGENCY, array('`add_time` LIKE '=>date('Y-m-d').'%'));
        $before_dealer_num    = user_helper::get_user_num_by_role_id(user_config::AGENCY, array('`add_time` < '=>date('Y-m-d').' 00:00:00'));

        $admin_data_list['dealer_data']['today_add_dealer_num'] = $today_add_dealer_num;
        $admin_data_list['dealer_data']['dealer_growth_rate']   = round(($today_add_dealer_num / $before_dealer_num) * 100, 2);

        // 成交订单数/已收到款项
        $admin_data_list['dealer_data']['dealer_order_num']   = order_helper::get_order_num_by_progress(array(3, 5, 10), 1, array('is_enable'=>1, 'status'=>1));
        $admin_data_list['dealer_data']['dealer_order_money'] = order_helper::get_order_money_by_progress(array(3, 5, 10), 1,  array('is_enable'=>1, 'status'=>1));

        // 已成功出货：多少箱 (总部向合伙人发货)
        $admin_data_list['dealer_data']['delivery_goods_num'] = order_helper::get_order_goods_num(array(5, 10), 1, array('is_enable'=>1, 'status'=>1));

        // 待收款订单/等待收款项：订单数量/（未成交金额）多少元
        $admin_data_list['dealer_data']['prepare_dealer_order_num']   = order_helper::get_order_num_by_progress(1, 1, array('is_enable'=>1, 'status'=>1));
        $admin_data_list['dealer_data']['prepare_dealer_order_money'] = order_helper::get_order_money_by_progress(1, 1, array('is_enable'=>1, 'status'=>1));

        // 等待发货：多少箱 (只要提交订单就算是)
        $admin_data_list['dealer_data']['prepare_delivery_order_num'] = order_helper::get_order_goods_num(array(1, 3), 1, array('is_enable'=>1, 'status'=>1));

        // 合伙人总库存：多少箱
        $dealer_ids                          = user_helper::get_role_user_ids(user_config::AGENCY, '' , array('is_enable'=>1));
        $admin_data_list['dealer_data']['all_dealer_stock'] = dealers_helper::get_dealer_stock($dealer_ids);

        // 合伙人总员工：多少人 (市场人员的总数量)
//         $store_list = store_helper::get_store_list_by_dealer_id($dealer_ids, array('is_enable'=>1, 'is_delete'=>1 ));
//         $marketing_ids = array();
//         if ($store_list) {
//             foreach ($store_list as $k=>$v) {
//                 if (!in_array($v['marketing_id'], $marketing_ids) && $v['marketing_id'] != $v['agency_id'] && $v['marketing_id'] != 0) {
//                     array_push($marketing_ids, $v['marketing_id']);
//                 }
//             }
//         }
        $enable_user_ids = _model('user')->getFields('id', array('status'=>1, 'is_enable'=>1));
        $marketing_num = _model('user_marketing')->getTotal(array('status'=>1, 'is_delete'=>1, 'marketing_id'=>$enable_user_ids));
        $admin_data_list['dealer_data']['marketing_num'] = $marketing_num;

        // 市场人员信息暂时不统计
//       市场人员相关数据
//         $admin_marketing_num = user_helper::get_format_user_num_by_role_id(user_config::MARKETING);
//         $admin_marketing_percent = 100 * round($admin_marketing_num / $admin_total_user_num, 4) . '%';
        // 统计市场人员处理的订单数
        // $admin_marketing_order_num = order_helper::get_marketing_order_num();

        // 终端店铺相关数据(分店)
//         $admin_data_list['store_num'] = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, array('is_enable'=>1));
        $admin_data_list['store_data']['store_num'] = store_helper::get_all_store_num();
        // 今日新增：多少门店增长率多少
        $today_add_store_num = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, array('`add_time` LIKE '=>date('Y-m-d').'%'));
        $admin_data_list['store_data']['today_add_store_num'] = $today_add_store_num;
        $before_store_num = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, array('`add_time` < '=>date('Y-m-d').' 00:00:00'));
        $admin_data_list['store_data']['store_growth_rate'] = round($today_add_store_num / $before_store_num * 100, 2);
        // $admin_store_order_num = order_helper::get_order_num_by_progress(10, 5);
        // 已进货门店数量：(必须是确认收货的门店)
        $admin_data_list['store_data']['is_stock_store_num'] = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, array('`score` > '=>0));
        // 未进货门店数量：(其他)
        $admin_data_list['store_data']['not_stock_store_num'] = $admin_data_list['store_data']['store_num'] - $admin_data_list['store_data']['is_stock_store_num'];
        // 门店订单总量：(所有订单)
        $admin_data_list['store_data']['store_order_num'] = order_helper::get_order_num_by_progress(array(1, 3, 5, 10, 15), 5, array('status'=>1, 'is_enable'=>1));
        // 今日新增订单量：(统计处在配送中的订单,不会撤销的订单)
        $admin_data_list['store_data']['store_new_order_num'] = order_helper::get_order_num_by_progress(array(5, 10), 5, array('status'=>1, 'is_enable'=>1));
        
        // $this->save_admin_data($admin_data_list);
        return $admin_data_list;
    }

    /**
     * 按照时间返回对应的日，周，月，统计用
     * @param unknown_type $type
     * @param unknown_type $time
     * @return string
     */
    private function get_date($type, $time = '')
    {
        if (!$time)  $time = time();

        if ($type == 'day') {
            $date = date('Y-m-d', $time);
        } else if ($type == 'week') {
            // 第几周
            $date = strftime('%Y%W', $time);
        } else if ($type == 'month') {
            $date = date('Ym', $time);
        }
        return $date;
    }
    
    /**
     * 保存记录
     */
    private function save_admin_data($data_list)
    {
        if (!$data_list) {
            return false;
        }
        
        $list = array();
        $list['dealer'] = array();
        $list['store'] = array();
        
        $data_list['dealer_data']['add_time'] = date('Y-m-d H:i:s');
        $data_list['store_data']['add_time'] = date('Y-m-d H:i:s');
        $day_time = $this->get_date('day');
        $week_time = $this->get_date('week');
        $month_time = $this->get_date('month');
        
        // 记录经销商相关统计数据
        $list['dealer'] = array();
        $list['dealer']['day']   = $data_list['dealer_data'];
        $list['dealer']['week']  = $data_list['dealer_data'];
        $list['dealer']['month'] = $data_list['dealer_data'];
        $list['dealer']['day']['date']   = $day_time;
        $list['dealer']['week']['date']  = $week_time;
        $list['dealer']['month']['date'] = $month_time;
        
        // 记录店铺相关统计数据
        $list['store'] = array();
        $list['store']['day']   = $data_list['store_data'];
        $list['store']['week']  = $data_list['store_data'];
        $list['store']['month'] = $data_list['store_data'];
        $list['store']['day']['date']   = $day_time;
        $list['store']['week']['date']  = $week_time;
        $list['store']['month']['date'] = $month_time;
        
        foreach ($list as $key => $data) {
            foreach ($data as $k => $v) {
                $cur_date = $this->get_date($k);
                $last_info = _model('admin_'.$key.'_data_'.$k)->read(array('date'=>$cur_date), 'ORDER BY `id` DESC');
                if ($last_info && $cur_date == $v['date']) {
                    _model('admin_'.$key.'_data_'.$k)->update($last_info['id'], $v);
                } else {
                    _model('admin_'.$key.'_data_'.$k)->create($v);
                }
            }
        }
        
    }



}

?>